package com.binvshe.binvshe.account.regist.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.ui.ServiceAgreementActivity;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.binvshe.binvshe.account.utils.RegularUtils;
import com.binvshe.binvshe.account.utils.StringFormatUtil;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.frameworkbase.customviews.IconTextArrowEditText;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegistPhoneFragment extends BaseFragment {


    @Bind(R.id.btnNext)
    Button mBtnNext;
    @Bind(R.id.tvProtocol)
    TextView mTvProtocol;
    @Bind(R.id.itetPhone)
    IconTextArrowEditText mItetPhone;

    public RegistPhoneFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        initToolBar();
        initHighLightStr(mTvProtocol);
        return view;
    }

    private void initToolBar() {
        getCustomToolbar().setVisibility(View.VISIBLE);
        getCustomToolbar().setTitle("注册");
        getCustomToolbar().enableBackButton().setOnClickListener(new BackBtnListener());
    }

    private void initHighLightStr(TextView str) {
        String wholeStr = str.getText() + "";
        String agreement = "《用户使用条款及服务协议》";
        StringFormatUtil spanStr = new StringFormatUtil(getContext(), wholeStr, agreement, R.color.user_protocol).fillColor();
        str.setText(spanStr.getResult());
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_regist_phone, null);
    }

    private String getPhone() {
        return mItetPhone.getEditText().trim();
    }

    @OnClick({R.id.btnNext, R.id.tvProtocol})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnNext:
                if (TextUtils.isEmpty(getPhone())) {
                    FragmentUtils.showWarnDialog("请输入手机号码", getFragmentManager());
                    return;
                } else if (!RegularUtils.isMobileSimple(getPhone())) {
                    FragmentUtils.showWarnDialog("请输入正确的手机号码", getFragmentManager());
                    return;
                }
                MyHttpRequest request = new MyHttpRequest(getContext());
                String urlExists = String.format(UrlCenter.EXISTS, getPhone());
                Log.e("TAG", "urlExists: " + urlExists);
                request.get(urlExists, null, new DataRequestListener<String>(String.class) {
                    @Override
                    public void onSuccess(String str) {
                        try {
                            JSONObject jsonObject = new JSONObject(str);
                            boolean isExist = jsonObject.optBoolean("value");
                            if (!isExist) {//该号码未被注册
                                FragmentUtils.addFragment(getFragmentManager(), R.id.registContainer, RegistCaptchaFragment.newInstance(getPhone()));
                            } else {
                                EasyToast.showToast(getContext() , "该号码已被注册");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onStart(Context context, String url) {
                        enableProgress(false);
                        super.onStart(context, url);
                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo, String strMsg) {
                    }
                });
                break;
            case R.id.tvProtocol:
                ServiceAgreementActivity.start(getContext());
                break;

        }
    }

    private class BackBtnListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            getActivity().finish();
        }

    }
}
